// pages/doortodoor/doortodoor.js
import { placeorderpage,appointmentTime,getRegion } from '../../../api/service'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opinionImages: '',
    classifyListOne: [],
    classifyListtwo: [],
    days: [],
    shenRegion: [],
    oneindex: -1,
    twoindex: -1,
    rankindex: -1,
    dayindex: -1,
    times: [],
    timeIndex: -1,
    privince: [],
    city: [],
    area: [],
    privinceId: -1,
    cityId: -1,
    areaId: -1,
    privincetext: '请选择省',
    citytext: '请选择市',
    areatext: '请选择区'
  },
  uploadImgs(e){
    var imgs = e.detail.picsList.join(",")
    this.setData({opinionImages:imgs})
  },
  getInitCon() {
    placeorderpage().then(res => {
      this.setData({
        classifyListOne:res.data.classifyListOne,
        classifyListtwo:res.data.classifyListtwo,
        days: res.data.days,
        shenRegion: res.data.shenRegion
      })
    })
  },
  switchOneIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      oneindex: index
    })
  },
  switchTwoIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      twoindex: index
    })
  },
  switchRankIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      rankindex: index
    })
  },
  swichDaysIndex(e) {
    const index = e.currentTarget.dataset.index
    const item = e.currentTarget.dataset.item
    const time = item.appointmentDate
    // this.setData({
    //   dayindex: index
    // })
    appointmentTime(time).then(res => {
      this.setData({
        times: res.data.timeList,
        dayindex: index,
        timeIndex: -1
      })
    })
  },
  switchTimeIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      timeIndex: index
    })
  },
  bindPrivincePickerChange(e) {
    const index = e.detail.value
    const id = this.data.privince[index].id
    this.setData({
      privinceId: id,
      privincetext: this.data.privince[index].name
    })
    this.getCity()
  },
  bindCityPickerChange(e) {
    const index = e.detail.value
    const id = this.data.city[index].id
    this.setData({
      cityId: id,
      citytext: this.data.city[index].name
    })
    this.getArea()
  },
  bindAreaPickerChange(e) {
    const index = e.detail.value
    const id = this.data.area[index].id
    this.setData({
      areaId: id,
      areatext: this.data.area[index].name
    })
  },
  getCity() {
    getRegion().then(res => {
      const id = this.data.privinceId
      const city = []
      res.data.forEach(item => {
        if(item.pid == id) {
          city.push(item)
        }
      })
      this.setData({
        city
      })
    })
  },
  getArea() {
    getRegion().then(res => {
      const id = this.data.cityId
      const area = []
      res.data.forEach(item => {
        if(item.pid == id) {
          area.push(item)
        }
      })
      this.setData({
        area
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInitCon()
    getRegion().then(res => {
      const privince = []
      const city = []
      const area = []
      res.data.forEach(item => {
        if(item.pid == 0) {
          privince.push(item)
        }
        // if(item.level == 2) {
        //   city.push(item)
        // }
        // if(item.level == 3) {
        //   area.push(item)
        // }
        this.setData({
          privince
          // city,
          // area
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})